<?php
/**
 *
 *
 * 功能说明：系统管理。
 *
 **/

namespace Qwadmin\Controller;

class SystemController extends ComController
{
    public function index()
    {

      if (!empty($_GET['catid'])) {
          
          $uname = M('member')->where(['ment'=>$_GET['catid']])->distinct(true)->field('post')->select();
          // dump($uname);
          // exit;
          if (!empty($uname)) {
            $this->ajaxReturn($uname);
          }


      }else{
          if (!empty($_GET['post'])) {
              // dump(I());
              // 
               $user = M('member')->where(['post'=>$_GET['post']])->distinct(true)->field('user')->select();
              // dump($user);
              // exit;
              if (!empty($user)) {
                $this->ajaxReturn($user);
              }
          }else{
             //查询部门及用户考核信息;
            $glist = M('auth_group')->where(['type'=>['neq','3']])->select();
            $ulist = M('member')->select();
            
            $db = M('zong');
            $count = $db->count();
            $pagecount = 15;
            $page = new \Think\Page($count , $pagecount);
            // $row[] = '';
            $page->parameter = $row; //此处的row是数组，为了传递查询条件
            $page->setConfig('first','第一页');
            $page->setConfig('prev','上一页');
            $page->setConfig('next','下一页');
            $page->setConfig('last','尾页');
            $page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% 第 '.I('p',1).' 页/共 %TOTAL_PAGE% 页 ( '.$pagecount.' 条/页 共 %TOTAL_ROW% 条)');
            $show = $page->show();
            $list = $db->order('uname desc')->limit($page->firstRow.','.$page->listRows)->select();
            // $list = M('zong')->join('qw_people as t1 on t1.uname = qw_zong.uname')->order('yue desc')->select();
            // foreach ($variable as $key => $value) {
            //   # code...
            // }
            $k_time = M('plan')->distinct(true)->field('j_time')->order('j_time desc')->select();
            $ment = M('plan')->distinct(true)->field('bu')->order('bu desc')->select();
            // dump($k_time);
            // exit;
            // $time = date('Y');
            $this->assign('ulist',$ulist);
            $this->assign('glist',$glist);
            $this->assign('page',$show);
            $this->assign('list',$list);
            $this->assign('k_time',$k_time);  //绩效月
            $this->assign('ment',$ment);   //考核部门
            // $this->assign('time',$time);
            $this->display();
          }
         
      }
    }



    public function eadd()
    {
      $model=M("plan");
      // dump(I());
      // exit;
      $plist['yue'] = substr($_POST['j_time'],5,2);
      // $yue = substr($_POST['j_time'],5,2);
      $plist['bu'] = $_POST['bu'];
      $plist['post'] = $_POST['post'];
      $plist['uname'] = $_POST['uname'];
      $glist = M('auth_group')->where(['title'=>$_POST['bu']])->find();
      $xyue = substr($_POST['j_time'],5,2);
      
      if ($xyue == 12) {
          $xgyue = '01';
          // $xgyue = $xyue+1;
      }else{
          $xgyue = $xyue+1;
      }
      // dump($xgyue);
      // exit;
      // dump($glist);
      // exit;
      // echo $yue;
      // exit;
      
    
      $llist = $model->where($plist)->select();
      // dump($llist);
      // exit;
      if (!empty($llist)) {

        echo "<script>alert('本月已导入".$plist['bu']."的绩效考核表,不要重复导入.');window.location.href='".__APP__."/System/index'</script>";
        exit;
      }
    
      
    if (! empty ( $_FILES ['file'] ['name']))
    {
      $tmp_file = $_FILES ['file'] ['tmp_name'];
      // echo $tmp_file;
      // exit;
      $file_types = explode (".",$_FILES ['file'] ['name'] );
      $file_type = $file_types[count( $file_types)-1];
      // if (strtolower( $file_type ) !== "xls" && strtolower( $file_type ) !== "xlsx" && strtolower( $file_type ) !== "csv"){
      //  echo "<script>alert('不是Excel文件，重新上传');window.location.href='".__APP__."/StorageOut/list'</script>";
        
      // }
      $savePath = './Public/upfile/Excel';

      $str = date ( 'Ymdhis' );
      $file_name = $str . "." . $file_type;
      if (! copy ($tmp_file, $savePath.$file_name)){
        echo("failed");
      }else{
        $excelpath=$savePath . $file_name;
        $tableCols = array(
          'angle','target','norm','weight', 'value','rule','ment','ment','j_time','time','j_name','type'
        );
        $data=importExcel($excelpath);
        $ulist = M('member')->where(['uid'=>$_SESSION['uid']])->find();

        // dump($_POST);
        // dump($data);
        // exit;
         $glist = M('auth_group')->where(['title'=>$_POST['bu']])->find();
          if (!empty($_POST['uname'])) {
                foreach ($data as $va) {
                  $list['angle'] = $va[0];
                  // $list['angle_t'] = $va[0];
                  // $mtype = substr($list['angle'],0,6);
                  $list['target'] = $va[1];
                  $list['norm'] = $va[2];
                  $list['weight'] = $va[3] * 100;
                  if (is_numeric($va[4])) {
                    $list['value'] = $va[4] * 100;
                  }else{
                    $list['value'] = $va[4] ;
                  }
                  $list['rule'] = $va[5];
                  $list['num'] = $va[6];
                  $list['bu'] = $_POST['bu'];
                  $list['uname'] = $_POST['uname'];
                  $list['post'] = $_POST['post'];
                  $list['j_time'] = $_POST['j_time'];
                  $list['time'] = date('Y-m-d');
                  $list['j_name'] = $ulist['user'];
                  $list['type'] = '绩效考核表';
                  // $list['yue'] =  substr($_POST['j_time'],5,2);
                  $list['yue'] =  substr($_POST['j_time'],5,2);
                  switch (substr($va[0],0,1)) {
                    case 'F':
                        $list['m_type'] = 1;
                      break;
                    case 'C':
                        $list['m_type'] = 2;
                      break;
                    case 'I':
                        $list['m_type'] = 3;
                      break;
                    case 'L':
                        $list['m_type'] = 4;
                      break;
                    
                  }
                  $list['g_type'] = $glist['type'];
                  $list['e_time'] = date('Y').'-'.$xgyue.'-'.'10';
                  // $list['num'] ++;
                  // dump(substr($va[0],0,1));
                  
                  $arr[] = $list;
                }

                // dump($arr);
                // exit;
                $ulist = M('member')->where(['user'=>$_POST['uname']])->field('user,ment,uid,post')->select();
                // dump($ulist);
                // exit;
                $yue = substr($_POST['j_time'],5,2);
                // echo $yue;
                // exit;
                

                


                // $pid[] = M('plan')->addALL($arr);
                // dump($pid);
                // exit;
                if (M('plan')->addALL($arr) > 0 ) {
                    $table = M('plan')->where(['uname'=>$arr[0]['uname'],'bu'=>$arr[0]['bu'],'yue'=>$arr[0]['yue']])->select();
                    // dump($table);
                    // exit;
                    foreach ($table as $key => $value) {
                      $pids .= $value['id'].",";
                    }
                    $pids = rtrim($pids,',');
                    // echo $pids;
                    // exit;
                    foreach ($ulist as  $va) {
                      $uu['uname'] = $va['user'];
                      $uu['bming'] = '绩效考核表';
                      $uu['ment'] = $va['ment'];
                      $uu['post'] = $va['post'];
                      $uu['uid'] = $va['uid'];
                      $uu['k_time'] = $_POST['j_time'];
                      $uu['e_time'] = date('Y').'-'.$xgyue.'-'.'10';
                      $uu['g_type'] = $glist['type'];
                      $uu['pid'] = $pids;
                      // $uu['time'] = date('Y-m-d');

                      $ae[] = $uu;
                    }
                    if (M('zong')->addALL($ae) > 0) {
                       
                          echo "<script>alert('导入成功');window.location.href='".__APP__."/System/jihua?uname=".$_POST['uname']."&yue=".$plist['yue']."'</script>";
                           
                    }
                       
                
                  
                }else{
                  echo "<script>alert('导入失败');window.location.href='".__APP__."/System/index'</script>";
                }         
          }else{    //------------------------------------------------------------------
                foreach ($data as $va) {
                  // dump($va);
                  
                  $list['angle'] = $va[0];
                  // $list['angle_t'] = $va[0];
                  // $mtype = substr($list['angle'],0,6);
                  $list['target'] = $va[1];
                  $list['norm'] = $va[2];
                  $list['weight'] = $va[3] * 100;
                  if (is_numeric($va[4])) {
                    $list['value'] = $va[4] * 100;
                  }else{
                    $list['value'] = $va[4] ;
                  }
                  
                  $list['rule'] = $va[5];
                  $list['num'] = $va[6];
                  $list['bu'] = $_POST['bu'];
                  $list['post'] = $_POST['post'];
                  $list['j_time'] = $_POST['j_time'];
                  $list['time'] = date('Y-m-d');
                  $list['j_name'] = $ulist['user'];
                  $list['type'] = '绩效考核表';
                  $list['yue'] =  substr($_POST['j_time'],5,2);
                  switch (substr($va[0],0,1)) {
                    case 'F':
                        $list['m_type'] = 1;
                      break;
                    case 'C':
                        $list['m_type'] = 2;
                      break;
                    case 'I':
                        $list['m_type'] = 3;
                      break;
                    case 'L':
                        $list['m_type'] = 4;
                      break;
                    
                  }
                  $list['g_type'] = $glist['type'];
                  $list['e_time'] = date('Y').'-'.$xgyue.'-'.'10';
                  // $list['num'] ++;
                  // dump(substr($va[0],0,1));
                  
                  $arr[] = $list;
                }

                // dump($arr);
                // exit;
                $ulist = M('member')->where(['post'=>$_POST['post']])->field('user,ment,uid,post')->select();
                // dump($ulist);
                // exit;
                $yue = substr($_POST['j_time'],5,2);
                // echo $yue;
                // exit;
                // foreach ($ulist as  $va) {
                //   $uu['uname'] = $va['user'];
                //   $uu['bming'] = '绩效考核表';
                //   $uu['ment'] = $va['ment'];
                //   $uu['post'] = $va['post'];
                //   $uu['uid'] = $va['uid'];
                //   $uu['k_time'] = $_POST['j_time'];
                //   $uu['e_time'] = date('Y').'-'.$xgyue.'-'.'10';
                //   $uu['g_type'] = $glist['type'];
                //   // $uu['g_type'] = $glist['type'];
                //   // $uu['time'] = date('Y-m-d');

                //   $ae[] = $uu;
                // }

                // dump($ae);
                // exit;

                // P($ae);
                // exit;
                if (M('plan')->addALL($arr) > 0 ) {
                  $table = M('plan')->where(['post'=>$arr[0]['post'],'bu'=>$arr[0]['bu'],'yue'=>$arr[0]['yue']])->select();
                  // dump($table);
                  // exit;
                  foreach ($table as $key => $value) {
                    $pids .= $value['id'].",";
                  }
                  $pids = rtrim($pids,',');
                  // echo $pids;
                  // exit;
                  foreach ($ulist as  $va) {
                    $uu['uname'] = $va['user'];
                    $uu['bming'] = '绩效考核表';
                    $uu['ment'] = $va['ment'];
                    $uu['post'] = $va['post'];
                    $uu['uid'] = $va['uid'];
                    $uu['k_time'] = $_POST['j_time'];
                    $uu['e_time'] = date('Y').'-'.$xgyue.'-'.'10';
                    $uu['g_type'] = $glist['type'];
                    $uu['pid'] = $pids;
                    // $uu['time'] = date('Y-m-d');

                    $ae[] = $uu;
                  }
                  if (M('zong')->addALL($ae) > 0) {
                        echo "<script>alert('导入成功');window.location.href='".__APP__."/System/jihua?bu=".$_POST['post']."&yue=".$plist['yue']."'</script>";
                  }
                       
                
                  
                }else{
                  echo "<script>alert('导入失败');window.location.href='".__APP__."/System/index'</script>";
                }         
          }
      
      }
    }else{
      echo "<script>alert('请上传文件');window.location.href='".__APP__."/System/index'</script>";
      
    }
    }







    //选择评分人界面
    public function jihua()
    {

      // dump(I());
      // exit;
      if (empty($_GET['uname'])) {
          $plist['yue'] = $_GET['yue'];

          $plist['post'] = $_GET['bu'];
          $gwei = '1';
          $jlist = M('plan')->where($plist)->select();
          // dump($jlist);
          // exit;
      }else{
          $plist['yue'] = $_GET['yue'];
          $gwei = '0';
          $plist['uname'] = $_GET['uname'];
          $jlist = M('plan')->where($plist)->select();
      }
    
      

        // $syue = $plist['yue'] - 1;
        // // echo $syue;
        // // exit;
        // // $cengname[''] = $
        // if (empty($plist['uname'])) {
        //     $cenglist['post'] = $plist['post'];
        //     $cenglist['yue'] = $syue;

        // }else{
        //     $cenglist['uname'] = $plist['uname'];
        //     $cenglist['yue'] = $syue;
        // }
        // dump($cenglist);
        // exit;
        $m = M('member')->where(['user'=>['neq','admin']])->select();
        $res = [];  

        foreach ($m as $key => $value) {
          $res[$value['ment']][] = $value;
        }
        // $pinglist = M('ping')->where($cenglist)->select();
        // // dump($pinglist);
        // // exit;
        // // if (!empty($pinglist[0]['pname'])) {
        // //       $pname = explode(',',$pinglist[0]['pname']) ;
        // // }
        // foreach ($pinglist as $key => $value) {
        //     $arr[] = $value['pname'];
        //     // $res[] = $arr;
        // }
        // dump($arr);
        // exit;
        // P($res);
        // exit;
        $this->assign('jlist',$jlist);
        $this->assign('gwei',$gwei);
        $this->assign('mlist',$res);
        $this->display();
    }





    //选定评分人
    public function xuan()
    {
      // P(I());
      // exit;
      // dump($_POST);
      // exit;
      // $plist = M('ping')->where(['pid']);
      $list = $_POST;
      // $list['time'] = date('Y-m-s');
       // P($list);
      //   exit;
        //获取表格类型;
        $blist = M('plan')->where(['id'=>$_POST['pid']])->find();

        $bming = $blist['type'];
        //截取数组最后一个字段值
      $users = array_pop($list);
      // P($users);
      // exit;
        $res   = [];
        foreach ($users as $user) {
            $arr = $list;
            $arr['time'] = date('Y-m-d');
            $arr['pname'] = $user;
              $res[]        = $arr;

            
        }
        // P($res);
        // exit;
        // dump($res);
        // exit;
        foreach ($res as $v) {
            // echo $v['n_id'];
            // exit;
            $pname .= $v['pname'].',';
        }

        if (empty($res[0]['uname'])) {
            $post = $res[0]['post'];
            $condition['post'] = $post; 
        }else{
            $post = $res[0]['uname']; 
            $condition['user'] = $post;
        }
        // $post = $res[0]['post']; 
        $yue = $res[0]['yue']; 

        $plist['pname'] = rtrim($pname,', ');
        $plist['pid'] = $res[0]['pid'];
        $fname['pname'] = rtrim($pname,', ');
        // P($plist);
        // exit;
        $dn['pname'] = rtrim($pname,', ');

        // dump($condition);
        // exit;
        // $condition['post'] = $ment;
        // $condition['_logic'] = 'OR';
        // $y['yue'] = $res[0]['yue']; 
        
        $ulist = M('member')->where($condition)->select();
        // dump($ulist);
        // exit;
      
      $arr = [];

      foreach ($ulist as $key => $v) {
        $arr[$key]['uname'] = $v['user'];
        $arr[$key]['uid'] = $v['uid'];
        // $arr[$key]['yue'] = $v['yue'];
        
      }

      // dump($arr);
      // exit;
      // P($arr);
      
      //获取评分人/被评分人/和考核表对于Id
      $pp = [];
      $bb = [];
      foreach ($arr as $key => $va) {
          $pp[$key]['pname'] = rtrim($pname,', ');
          $pp[$key]['pid'] = $res[0]['pid'];
          $pp[$key]['uname'] = $va['uname'];
          $pp[$key]['uid'] = $va['uid'];
          $pp[$key]['yue'] = $res[0]['yue'];
          $pp[$key]['ment'] = $res[0]['ment'];
          $pp[$key]['bming'] = $bming;
          $pp[$key]['m_type'] = $res[0]['m_type'];
          $pp[$key]['g_type'] = $res[0]['g_type'];
          $pp[$key]['angle'] = $res[0]['angle'];
          $pp[$key]['target'] = $res[0]['target'];
          $pp[$key]['norm'] = $res[0]['norm'];
          $pp[$key]['weight'] = $res[0]['weight'];
          $pp[$key]['value'] = $res[0]['value'];
          $pp[$key]['rule'] = $res[0]['rule'];
          $pp[$key]['num'] = $res[0]['num'];
          $pp[$key]['k_time'] = $res[0]['j_time'];
          $pp[$key]['e_time'] = $res[0]['e_time'];
          // $pp[$key]['num'] = $res[0]['num'];
          
      }
      // dump($res);
      // exit;
      // P($res);
      $data = [];
        foreach ($res as $v){
            foreach ($arr as $v1) {
                $aaa = [
                    'uname' => $v1['uname'],
                    'm_type' => $v['m_type'],
                    'g_type' => $v['g_type'],
                    'pid' => $v['pid'],
                    'pname' => $v['pname'],
                    'angle' => $v['angle'],
                    'target' => $v['target'],
                    'norm' => $v['norm'],
                    'weight' => $v['weight'],
                    'value' => $v['value'],
                    'rule' => $v['rule'],
                    'num' => $v['num'],
                    'ment' => $v['ment'],
                    'yue' => $v['yue'],
                    'time' => $v['time'],
                    'uid' => $v1['uid'],
                    'bming' => '绩效考核表',
                    'k_time' => $v['j_time'],
                    'e_time' => $v['e_time']
                ];
                $data[] = $aaa;
            }
        }
        // dump($pp);
        // exit;
        // dump($users);
        // dump($arr);
        // exit;
        // $names = [];
        // foreach ($arr as $value) {
        //  foreach ($users as $va) {
        //    $a['pname'] = $va;
        //    $a['uname'] = $value['uname'];
        //    $a['uid'] = $value['uid'];
        //    $names[] = $a;
        //  }
        // }
        // foreach ($data as $key => $value) {
        //   if ($value['m_type'] == '3') {
        //       $I[] = $value;
        //   }
        // }
        // foreach ($I as $key => $value) {
        //   if ($value['num'] == '集团财务中心') {
        //       $ilist[] = $value;
        //   }
        // }
        // foreach ($ilist as $key => $value) {
        //     $value['i'] = '1';
        // }
        foreach ($data as $key => $value) {
          if ($value['m_type'] == '1') {
              $value['i'] = '1';
              // $value['i'] = '1';
              // $adata[] = $value;
          }

           $adata[] = $value;
        }
        foreach ($adata as $key => $value) {
            if ($value['m_type'] == '3') {
              $I[] = $value;
            }
        }
        foreach ($I as $key => $value) {
          if ($value['num'] == '集团财务中心') {
              $value['i'] = '1';
              $ilist[] = $value;
          }

        }


        foreach ($pp as $key => $value) {
          if ($value['m_type'] == '1') {
              $value['i'] = '1';
              // $value['i'] = '1';
              // $adata[] = $value;
          }

           $pdata[] = $value;
        }
        foreach ($pdata as $key => $value) {
            if ($value['m_type'] == '3') {
              $Ip[] = $value;
            }
        }
        foreach ($Ip as $key => $value) {
          if ($value['num'] == '集团财务中心') {
              $value['i'] = '1';
              $iplist[] = $value;
          }

        }
        //  foreach ($ilist as $key => $value) {
        //     $value['i'] = '1';
        // }
        // P($data);
        // dump($pdata);
        // // // dump($pp);
        // exit;
        // dump($adata);
        // exit;
        if (M('plan')->where(['id'=>$_POST['pid']])->save($fname) > 0) {
          if (empty($ilist)) {
              M()->startTrans();
              foreach ($adata as $value) {
                  if (M('ping')->add($value) == 'false') {
                        M()->rollback();
                      return $this->error('添加失败');
                    }
                             
              }
              M()->commit();
          }else{
              M()->startTrans();
              foreach ($ilist as $value) {
                  if (M('ping')->add($value) == 'false') {
                        M()->rollback();
                      return $this->error('添加失败');
                    }
                             
              }
              M()->commit();
          }
           
          if (empty($iplist)) {
              M()->startTrans();
              foreach ($pdata as $value) {
                  if (M('people')->add($value) == 'false') {
                        M()->rollback();
                      return $this->error('添加失败');
                    }
                             
              }

              M()->commit();
          }else{
              M()->startTrans();
              foreach ($iplist as $value) {
                  if (M('people')->add($value) == 'false') {
                        M()->rollback();
                      return $this->error('添加失败');
                    }
                             
              }

              M()->commit();
          }
            

            echo '1111';
        }else{
          echo '2222';
        }
        
      


    }





    //对应uid显示考核表详情
    public function xiang()
    {
      // P(I());
      $uid = $_GET['uid'];
      $yue = $_GET['yue'];
      $list = $_GET;
      // echo $uid .'/'. $yue.'/'.$_GET['e_time'];
      // exit;
      $plist = M('people')->where(['uid'=>$uid,'k_time'=>$yue])->order('pid asc')->select();
      // dump($plist);
      // exit;
      $daytime = strtotime(date('Y-m-d'));
      $e_time = strtotime($_GET['e_time']);
      // echo $daytime;
      // echo '<br>';
      // echo $e_time;
      // exit;
      //得出评分为空的数据
      $resempty = [];
      foreach ($plist as $key => $value) {
          if (empty($value['result'])) {
              $pidlist .= "`pid`="."'".$value['pid']."'"." or ";
              // $resempty[] = $value;
          }
      }
      //查出评分为空的PID
      $pidlist = rtrim($pidlist,'or ');
      if (!empty($pidlist)) {
          if ($daytime > $_GET['e_time']) {
              $pinglist = M('ping')->where($pidlist)->select();
              //拿出内部客户满意度评分数据
              $C = [];
              foreach ($pinglist as $key => $value) {
                  if ($value['m_type'] == '2') {
                      $C[] = $value;
                  }
              }
              //查询内部客户满意度数据中评分不为空的
              $onearray = [];
              $towarray = [];
              foreach ($C as $key => $value) {
                  if (!empty($value['result'])) {
                      $onearray[] = $value['result'];
                      $towarray[] = $value;
                  }
              }
              if (!empty($towarray)) {
                  
                  // dump($towarray);
                  // exit;
                  //获取最高分和最低分
                  $pingmin = array_search(min($onearray),$onearray);
                  $pingmax = array_search(max($onearray),$onearray);
                  // echo $onearray[$pingmin];
                  // echo $onearray[$pingmax];
                  // exit;
                  // $minlist = [];
                  //去除最低分
                  foreach ($towarray as $key => $value) {
                      if ($value['result'] == $onearray[$pingmin]) {
                          unset($towarray[$key]);
                          break;
                      }
                  }

                  //去除最高分
                  foreach ($towarray as $key => $value) {
                      if ($value['result'] == $onearray[$pingmax]) {
                          unset($towarray[$key]);
                          break;
                      }
                  }
                  //查询评分人
                  foreach ($towarray as $key => $value) {
                      $pusers .= "`user`="."'".$value['pname']."'"." or ";
                  }
                  $pusers = rtrim($pusers,'or ');
                  $pnames = M('member')->where($pusers)->select();
                  $jnames = [];
                  $znames = [];
                  $gznames = [];
                  foreach ($pnames as $key => $value) {
                      if ($value['class'] == '2') {
                           $gnames[] = $value['user'];
                      }
                      if ($value['class'] == '1') {
                           $znames[] = $value['user'];
                      }
                      if ($value['class'] == '0') {
                           $jnames[] = $value['user'];
                      }
                  }
                  //得出高层评分
                  $gfen = [];
                  $zfen = [];
                  $jfen = [];
                  foreach ($towarray as $key => &$value) {
                      if (in_array($value['pname'],$gnames)) {
                          $gfen[] = $value;
                      }
                      if (in_array($value['pname'],$znames)) {
                          $zfen[] = $value;
                      }
                      if (in_array($value['pname'],$jnames)) {
                          $jfen[] = $value;
                      }
                  }
                  $gsum = count($gfen);
                  foreach ($gfen as $key => $value) {
                      $gresults[0]['result'] += $value['result'] / $gsum * 0.5;
                  }
                  $zsum = count($zfen);
                  foreach ($zfen as $key => $value) {
                      $zresults[0]['result'] += $value['result'] / $zsum * 0.3;
                  }
                  $jsum = count($jfen);
                  foreach ($jfen as $key => $value) {
                      $jresults[0]['result'] += $value['result'] / $jsum * 0.2;
                  }
                  //总和所有评分人分数
                  $zongfen =  array_merge($gresults,$zresults,$jresults);
                  foreach ($zongfen as $key => $value) {
                      $result += $value['result'];
                  }
                  //计算标准分
                  if ($result > '85' && $result < '95') {
                     $cha = 95 - $result;
                     // echo $cha;
                     $que = $cha * 10;
                     $standard = 95 - $que;
                 
                  }else if($result >= '95'){
                              $standard = '100';
                  }else if($result <= '85'){
                              $standard = '0';
                  }
                  //得出考核得分
                  $weights = M('ping')->where(['pid'=>$C[0]['pid']])->find();
                  $weight = $weights['weight'].'%';
                  $score = $standard / $weight;
                  $sping['result'] = $result;
                  $sping['standard'] = $standard;
                  $sping['score'] = $score;
                  M('people')->where(['uid'=>$uid,'e_time'=>$_GET['e_time'],'m_type'=>'2',$C[0]['pid']])->save($sping);
              }
              
              $this->assign('list',$plist);
              $this->assign('uname',$uname);
              $this->display();
          }else{
            $this->assign('list',$plist);
            $this->assign('uname',$uname);
            $this->display();
          }
      }else{
          $this->assign('list',$plist);
          $this->assign('uname',$uname);
          $this->display();
      }
      // exit;
      
      // exit;
      
      
      
    }







    public function she()
    {
        //查询部门及用户考核信息;
        // $glist = M('auth_group')->select();
        // $ulist = M('member')->select();
        
        $list = M('zong')->select();

        $time = date('Y');
        // $this->assign('ulist',$ulist);
        // $this->assign('glist',$glist);
        $this->assign('list',$list);
        $this->assign('time',$time);
        $this->display();
    }


    public function shtml()
    {
        if (IS_POST) {
            // P(I());
            $list['standard'] = $_POST['standard'];
            $list['score'] = $_POST['score'];
            if (M('people')->where(['id'=>$_POST['id']])->save($list) > 0) {
                $this->success('设置成功','she');
            }else{
                $this->error('设置失败');
            }
        }else{
            // P(I());
            $list = M('people')->where(['uid'=>$_GET['uid'],'yue'=>$_GET['yue'],'m_type'=>'2'])->select();


            // P($list);
            $this->assign('list',$list);
            $this->display();
        }


    }



    public function ping()
    {
      if (IS_POST) {
        // dump(I());
        // exit;
        switch ($_GET['yue']) {
          case '财务指标':
              $uname = M('member')->where(['ment'=>'集团绩效管理委员会x'])->select();
            break;
          case '学习成长':
              $uname = M('member')->where(['ment'=>'集团财务中心'])->select();
            break;
          
        }
      }else{
        // $list = M('plan')->where(['m_type'=>['in',array(1,4)]])->distinct(true)->field('m_type,yue,uname')->select();
        // // dump($list);
        // // exit;
        $cai = M('member')->where(['ment'=>'集团财务中心'])->select();
        $xue = M('member')->where(['ment'=>['in',array('远大商学院','集团绩效管理委员会')]])->select();
        $clist = M('she')->where(['type'=>'1'])->select();
        // dump($clist);
        // exit;
         
        // if (!empty($clist[0]['name'])) {
        //       $pname = explode(',',$clist[0]['name']) ;
        // }
        
        $xlist = M('she')->where(['type'=>'2'])->select();
        if (!empty($xlist[0]['name'])) {
              $pname = explode(',',$xlist[0]['name']) ;
        }
        foreach ($pname as $key => $value) {
            $xarr[] = $value;
            // $res[] = $arr;
        }
        $clist = M('she')->where(['type'=>'1'])->select();
        if (!empty($clist[0]['name'])) {
              $cpname = explode(',',$clist[0]['name']) ;
        }
        foreach ($cpname as $key => $value) {
            $carr[] = $value;
            // $res[] = $arr;
        }
        // dump($xue);
        // dump($arr);
        // exit;
        $this->assign('cai',$cai);
        $this->assign('xue',$xue);
        $this->assign('clist',$clist);
        $this->assign('xlist',$xlist);
        $this->assign('arr',$xarr);
        $this->assign('carr',$carr);
        $this->display();
      }
    }





    //判断是否有考核人
    public function pan()
    {
      // dump(I());
      // exit;
      // 如果选择的是岗位
      if (empty($_POST['uname'])) {
          foreach ($_POST['pid'] as  $va) {
              // dump($va);
              $pid .= "`id`="."'".$va."'"." or ";
          }
          $pid = rtrim($pid,'or ');
          $plist = '('.$pid.')'.' and `post`='.'\''.$_POST['post'].'\'';
          // echo $plist;
          $ping = M('plan')->where($plist)->select();
          $ulist = M('member')->where(['post'=>$_POST['post']])->distinct(true)->field('user,ment,uid,lead')->select();
          $F = [];
          $C = [];
          $I = [];
          $L = [];
          //判断FCIL是否为空,将为空的添加上评分人
          foreach ($ping as  $value) {
              if ($value['pname'] == null) {
                  //遍历F数据
                  if ($value['m_type'] == '1') {
                      $F[] = $value;
                      $sname = M('she')->where(['type'=>1])->find(); 
                   } 

                   //遍历C数据
                  if ($value['m_type'] == '2') {
                      $C[] = $value;
                   }
                   //遍历I数据
                  if ($value['m_type'] == '3') {
                      $I[] = $value;
                   }
                  if ($value['m_type'] == '4' ) {
                      $L[] = $value;
                      $sname2 = M('she')->where(['type'=>2])->find();
                   }   
              }
          }

          
          // // dump($sxy);
          // dump($I);
          // exit;
          //F  添加评分人 整合数组;
          $flist = [];
          foreach ($F as $fey => $f) {
              foreach ($ulist as $uey => $u) {
                  $tmp = $f;
                  $tmp['pname'] = $sname['name'];
                  $tmp['uname'] = $u['user'];
                  $tmp['ment'] = $u['ment'];
                  $tmp['uid'] = $u['uid'];
                  $tmp['i'] = '1';
                  $tmp['bming'] = '绩效考核表';
                  // $tmp['m_type'] = '1';
                  $tmp['pid'] = $f['id'];
                  $tmp['k_time'] = $f['j_time'];
                  unset($tmp['id']);
                  unset($tmp['bu']);
                  unset($tmp['type']);
                  unset($tmp['j_time']);
                  unset($tmp['j_name']);
                  $flist[] = $tmp;
              }
          }
          
          //C  添加评分人 整合数组
          $clist = [];
          foreach ($C as $cey => $c) {
              foreach ($ulist as $uey => $u) {
                  $ctmp = $c;
                  $ctmp['xname'] = $u['lead'];
                  $ctmp['pname'] = '';
                  $ctmp['uname'] = $u['user'];
                  $ctmp['ment'] = $u['ment'];
                  $ctmp['bming'] = '绩效考核表';
                  $ctmp['uid'] = $u['uid'];
                  $ctmp['k_time'] = $c['j_time'];
                  // $tmp['m_type'] = '1';
                  $ctmp['pid'] = $c['id'];
                  unset($ctmp['id']);
                  unset($ctmp['bu']);
                  unset($ctmp['time']);
                  unset($ctmp['type']);
                  unset($ctmp['j_time']);
                  unset($ctmp['j_name']);
                  $clist[] = $ctmp;
              }
              
          }
          // dump($clist);
          // exit;
          //I 添加评分人 整合数组；
          foreach ($I as $iey => $vi) {
              if ($vi['num'] == '集团财务中心') {
                  $cai[] = $vi;
              }
              if ($vi['num'] != '集团财务中心') {
                  $qita[] = $vi;
              }
          }
          //I  添加评分人 整合数组
          $ilist = [];
          foreach ($qita as $iey => $i) {
              foreach ($ulist as $uey => $u) {
                  $itmp = $i;
                  $itmp['pname'] = $u['lead'];
                  $itmp['uname'] = $u['user'];
                  $itmp['ment'] = $u['ment'];
                  $itmp['bming'] = '绩效考核表';
                  $itmp['uid'] = $u['uid'];
                  $itmp['pid'] = $i['id'];
                  $itmp['k_time'] = $i['j_time'];
                  unset($itmp['id']);
                  unset($itmp['bu']);
                  unset($itmp['time']);
                  unset($itmp['type']);
                  unset($itmp['j_time']);
                  unset($itmp['j_name']);
                  $ilist[] = $itmp;
              }
          }
          $iclist = [];
          foreach ($cai as $cey => $c) {
              foreach ($ulist as $uey => $u) {
                  $ictmp = $c;
                  $ictmp['pname'] = $sname['name'];
                  $ictmp['uname'] = $u['user'];
                  $ictmp['ment'] = $u['ment'];
                  $ictmp['bming'] = '绩效考核表';
                  $ictmp['uid'] = $u['uid'];
                  $ictmp['i'] = '1';
                  // $tmp['m_type'] = '1';
                  $ictmp['pid'] = $c['id'];
                  $ictmp['k_time'] = $c['j_time'];
                  unset($ictmp['id']);
                  unset($ictmp['bu']);
                  unset($ictmp['time']);
                  unset($ictmp['type']);
                  unset($ictmp['j_time']);
                  unset($ictmp['j_name']);
                  $iclist[] = $ictmp;
              }
              
          }
          // dump($L);
          // exit;

          //L  添加评分人 整合数组;
          foreach ($L as $ley => $vl) {
              if ($vl['num'] == '远大商学院') {
                  $sxy[] = $vl;
              }
              if ($vl['num'] == '集团绩效管理委员会') {
                  $jx[] = $vl;
              }
              if ($vl['num'] != '集团绩效管理委员会' && $vl['num'] != '远大商学院') {
                  $qt[] = $vl;
              }
          }

          // dump($sxy);
          // exit;
          //拆分学习成长默认设置评分人
          $suser = explode(",",$sname2['name']); 
          // dump($suser);
          // exit;
          $sheuser = [];
          foreach ($suser as $key => $value) {
              $sheuser[$key]['user'] = $value;
          }
          $susers = [];
          $jusers = [];
          foreach ($sheuser as $key => $value) {
              $userlist = M('member')->where($value)->select();
              // dump($userlist);
              foreach ($userlist as $ukey => $uval) {
                  if ($uval['ment'] == '远大商学院') {
                    $susers[] = $uval;
                  }
                  if ($uval['ment'] == '集团绩效管理委员会') {
                      $jusers[] = $uval;
                  }
              }
          }
          //得到学习成长评分人
          // dump($susers);
          // dump($jusers);
          // exit;
          // dump($sxy);
          // dump($jx);
          // exit;
          //
          //数据来源-商学院
          $slist = [];
          foreach ($sxy as $sey => $s) {
              foreach ($ulist as $uey => $u) {
                  $stmp = $s;
                  $stmp['pname'] = $sname2['name'];
                  $stmp['uname'] = $u['user'];
                  $stmp['ment'] = $u['ment'];
                  $stmp['uid'] = $u['uid'];
                  $stmp['bming'] = '绩效考核表';
                  // $tmp['m_type'] = '1';
                  $stmp['pid'] = $s['id'];
                  $stmp['k_time'] = $s['j_time'];
                  unset($stmp['id']);
                  unset($stmp['bu']);
                  unset($stmp['type']);
                  unset($stmp['j_time']);
                  unset($stmp['j_name']);
                  $slist[] = $stmp;
              }
              
          }
          //数据来源-绩效部门
          $jlist = [];
          foreach ($jx as $jey => $j) {
              foreach ($ulist as $uey => $u) {
                  $jtmp = $j;
                  $jtmp['pname'] = $sname2['name'];
                  $jtmp['uname'] = $u['user'];
                  $jtmp['ment'] = $u['ment'];
                  $jtmp['uid'] = $u['uid'];
                  $jtmp['bming'] = '绩效考核表';
                  // $tmp['m_type'] = '1';
                  $jtmp['pid'] = $j['id'];
                  $jtmp['k_time'] = $j['j_time'];
                  unset($jtmp['id']);
                  unset($jtmp['bu']);
                  unset($jtmp['type']);
                  unset($jtmp['j_time']);
                  unset($jtmp['j_name']);
                  $jlist[] = $jtmp;
              }
              
          }
         //其他部门
         $qlist = [];
          foreach ($qt as $qey => $q) {
              foreach ($ulist as $uey => $u) {
                  $qtmp = $q;
                  $qtmp['pname'] = $sname2['name'];
                  $qtmp['uname'] = $u['user'];
                  $qtmp['ment'] = $u['ment'];
                  $qtmp['uid'] = $u['uid'];
                  $qtmp['bming'] = '绩效考核表';
                  // $tmp['m_type'] = '1';
                  $qtmp['pid'] = $q['id'];
                  $qtmp['k_time'] = $q['j_time'];
                  unset($qtmp['id']);
                  unset($qtmp['bu']);
                  unset($qtmp['type']);
                  unset($qtmp['j_time']);
                  unset($qtmp['j_name']);
                  $qlist[] = $qtmp;
              }
              
          }
          // if (!empty($slist)) {
                
          // }
            // dump($sxy);
            // echo '<hr>';
            // dump($slist);
            // exit;
            //如果数据来源有商学院
            if (!empty($slist)) {
                $sres = [];
                foreach ($slist as $sey => $sv) {
                    foreach ($susers as $ukey => $su) {
                          $sutmp = $sv;
                          unset($sutmp['pname']);
                          $sutmp['pname'] =  $su['user'];
                          $sres[] = $sutmp;
                    }
                }

                M()->startTrans();
                foreach ($sres as $value) {
                    if (M('ping')->add($value) == 'false') {
                        M()->rollback();
                        return $this->error('添加失败');
                    }
                 
                }
                M()->commit();
                foreach ($susers as $key => $value) {
                    $snames .= $value['user'].',';
                }
                $snames = rtrim($snames,', ');
                $spusers = [];
                foreach ($slist as $key => $value) {
                    $sptmp = $value;
                    $sptmp['pname'] = $snames;
                    $spusers[] = $sptmp;
                  
                }
                 M()->startTrans();
                foreach ($spusers as $value) {
                    if (M('people')->add($value) == 'false') {
                        M()->rollback();
                        return $this->error('添加失败');
                    }
                 
                }
                M()->commit();
                //如果数据来源有绩效
                if (!empty($jlist)) {
                    $jres = [];
                    foreach ($jlist as $jey => $jv) {
                        foreach ($jusers as $jkey => $ju) {
                              $jutmp = $jv;
                              unset($jutmp['pname']);
                              $jutmp['pname'] =  $ju['user'];
                              $jres[] = $jutmp;
                        }
                    }

                    M()->startTrans();
                    foreach ($jres as $value) {
                        if (M('ping')->add($value) == 'false') {
                            M()->rollback();
                            return $this->error('添加失败');
                        }
                     
                    }
                    M()->commit();
                    foreach ($jusers as $key => $value) {
                        $jnames .= $value['user'].',';
                    }
                    $jnames = rtrim($jnames,', ');
                    $jpusers = [];
                    foreach ($jlist as $key => $value) {
                        $jptmp = $value;
                        $jptmp['pname'] = $jnames;
                        $jpusers[] = $jptmp;
                      
                    }
                     M()->startTrans();
                    foreach ($jpusers as $value) {
                        if (M('people')->add($value) == 'false') {
                            M()->rollback();
                            return $this->error('添加失败');
                        }
                     
                    }
                    M()->commit();
                }
            }else if(!empty($jlist)){ //如果数据来源有绩效
                          $jres = [];
                          foreach ($jlist as $jey => $jv) {
                              foreach ($jusers as $jkey => $ju) {
                                    $jutmp = $jv;
                                    unset($jutmp['pname']);
                                    $jutmp['pname'] =  $ju['user'];
                                    $jres[] = $jutmp;
                              }
                          }

                          M()->startTrans();
                          foreach ($jres as $value) {
                              if (M('ping')->add($value) == 'false') {
                                  M()->rollback();
                                  return $this->error('添加失败');
                              }
                           
                          }
                          M()->commit();
                          foreach ($jusers as $key => $value) {
                              $jnames .= $value['user'].',';
                          }
                          $jnames = rtrim($jnames,', ');
                          $jpusers = [];
                          foreach ($jlist as $key => $value) {
                              $jptmp = $value;
                              $jptmp['pname'] = $jnames;
                              $jpusers[] = $jptmp;
                            
                          }
                           M()->startTrans();
                          foreach ($jpusers as $value) {
                              if (M('people')->add($value) == 'false') {
                                  M()->rollback();
                                  return $this->error('添加失败');
                              }
                           
                          }
                          M()->commit();
            }
            // dump($jpusers);
            // // echo $snames;
            // exit;
          // //如果F评分人为空,添加评分人
          if (!empty($flist)) {
              M()->startTrans();
              foreach ($flist as $value) {
                  if (M('people')->add($value) == 'false') {
                      M()->rollback();
                      return $this->error('添加失败');
                  }
               
              }
              M()->commit();
              $fsname = explode(",",$sname['name']);
              // exit;
              $fshename = [];
              foreach ($fsname as $key => $ss) {
                    $fshename[$key]['pname'] = $ss;
              }
              $ffres = [];
              foreach ($flist as $fkey => $ff) {
                  foreach ($fshename as $fkey => $she) {
                      $fres = $ff;
                      unset($fres['pname']);
                      $fres['pname'] = $she['pname'];
                      $ffres[] = $fres;
                  }
              }
              M()->startTrans();
              foreach ($ffres as $value) {
                  if (M('ping')->add($value) == 'false') {
                      M()->rollback();
                      return $this->error('添加失败');
                  }
               
              }
              M()->commit();
              foreach ($F as  $vid) {
                 $id .= "`id`="."'".$vid['id']."'"." or ";
              }
              $id = rtrim($id,'or ');
              $planname['pname'] = $sname['name'];
              if (M('plan')->where($id)->save($planname) > 0) {
                  echo '1111';
              }
          }
          //F    ------------------------END;
              
          // //如果C评分人为空,添加评分人
          if (!empty($clist)) {
              M()->startTrans();
              foreach ($clist as $value) {
                  if (M('people')->add($value) == 'false') {
                      M()->rollback();
                      return $this->error('添加失败');
                  }
               
              }
              M()->commit();
             
          }
          //C    ------------------------END;

      // //如果I评分人为空,添加评分人
          if (!empty($ilist)) {
              foreach ($ilist as  $vid) {
                 $iid .= "`id`="."'".$vid['pid']."'"." or ";
              }
              $iid = rtrim($iid,'or ');
              // echo $iid;
              // exit;
              M()->startTrans();
              foreach ($ilist as $ikey => $value) {
                  if (M('ping')->add($value) == 'false') {

                      M()->rollback();
                      return $this->error('添加失败');
                  }
                  if (M('people')->add($value) == 'false') {
                      M()->rollback();
                      return $this->error('添加失败');
                  }
                  $name[] = $value['pname'];
              }
              M()->commit();
              $namet = array_unique($name);
              // $iidt = array_unique($iid);
              $ipname['pname'] = implode(',',$namet);
              if (M('plan')->where($iid)->save($ipname) > 0) {
                  echo '1111';
              }
          }

          if (!empty($iclist)) {
              foreach ($iclist as  $vid) {
                 $icid .= "`id`="."'".$vid['pid']."'"." or ";
              }
              $icid = rtrim($icid,'or ');
              // echo $iid;
              // exit;
              M()->startTrans();
              foreach ($iclist as $ikey => $value) {
                  if (M('ping')->add($value) == 'false') {

                      M()->rollback();
                      return $this->error('添加失败');
                  }
                  if (M('people')->add($value) == 'false') {
                      M()->rollback();
                      return $this->error('添加失败');
                  }
                  $cname[] = $value['pname'];
              }
              M()->commit();
              $cnamet = array_unique($cname);
              // $iidt = array_unique($iid);
              $icpname['pname'] = implode(',',$cnamet);
              // echo $iid;
              // dump($ipname);
              // exit;
              if (M('plan')->where($icid)->save($icpname) > 0) {
                  echo '1111';
              }
          }
          //I    ------------------------END;
          


          // //如果L评分人为空,添加评分人
          // if (!empty($llist)) {
          //     M()->startTrans();
          //     foreach ($llist as $value) {
          //         if (M('people')->add($value) == 'false') {
          //             M()->rollback();
          //             return $this->error('添加失败');
          //         }
               
          //     }
          //     M()->commit();
          //     $ssname = explode(",",$sname2['name']);
          //     // exit;
          //     $shename = [];
          //     foreach ($ssname as $key => $ss) {
          //           $shename[$key]['pname'] = $ss;
          //     }
          //     $res = [];
          //     foreach ($llist as $likey => $li) {
          //         foreach ($shename as $skey => $she) {
          //             $lres = $li;
          //             unset($lres['pname']);
          //             $lres['pname'] = $she['pname'];
          //             $res[] = $lres;
          //         }
          //     }
          //     M()->startTrans();
          //     foreach ($res as $value) {
          //         if (M('ping')->add($value) == 'false') {
          //             M()->rollback();
          //             return $this->error('添加失败');
          //         }
               
          //     }
          //     M()->commit();
          //     foreach ($L as  $vid) {
          //        $lid .= "`id`="."'".$vid['id']."'"." or ";
          //     }
          //     $lid = rtrim($lid,'or ');
          //     $lllname['pname'] = $sname2['name'];
          //     if (M('plan')->where($lid)->save($lllname) > 0) {
          //         echo '1111';
          //     }
          // }
          //L    ------------------------END;
          

          //获得没有评分人的考核
          // dump($ulist);
          $this->redirect('System/index');
        // $plist = ($pid).'and post='.$_POST['post'];
      }else{
              $ulist = M('member')->where(['user'=>$_POST['uname']])->distinct(true)->field('user,ment,uid,lead')->select();
              foreach ($_POST['pid'] as  $va) {
                  // dump($va);
                  $pid .= "`id`="."'".$va."'"." or ";
              }
              $pid = rtrim($pid,'or ');
              // echo $pid;
              // exit;
              $plist = '('.$pid.')'.' and `uname`='.'\''.$_POST['uname'].'\'';
              // echo $plist;
              // exit;
              $ping = M('plan')->where($plist)->select();
              // dump($ulist);
              // exit;
              $F = [];
              $C = [];
              $I = [];
              $L = [];
              //判断FCIL是否为空,将为空的添加上评分人
              foreach ($ping as  $value) {
                  if ($value['pname'] == null) {
                      //遍历F数据
                      if ($value['m_type'] == '1') {
                          $F[] = $value;
                          // $F['panme'] = 张威;
                          
                          $sname = M('she')->where(['type'=>1])->find();
                          
                       } 

                       //遍历C数据
                      if ($value['m_type'] == '2') {
                          $C[] = $value;
                       }
                       //遍历I数据
                      if ($value['m_type'] == '3') {
                          $I[] = $value;
                       }
                      if ($value['m_type'] == '4') {
                          $L[] = $value;
                          $sname2 = M('she')->where(['type'=>2])->find();
                       }   
                  }
              }

              //L  添加评分人 整合数组;
              foreach ($L as $ley => $vl) {
                  if ($vl['num'] == '远大商学院') {
                      $sxy[] = $vl;
                  }
                  if ($vl['num'] == '集团绩效管理委员会') {
                      $jx[] = $vl;
                  }
                  if ($vl['num'] != '集团绩效管理委员会' && $vl['num'] != '远大商学院') {
                      $qt[] = $vl;
                  }
              }
              
          
          
          //拆分学习成长默认设置评分人
          $suser = explode(",",$sname2['name']); 
          // dump($suser);
          // exit;
          $sheuser = [];
          foreach ($suser as $key => $value) {
              $sheuser[$key]['user'] = $value;
          }
          $susers = [];
          $jusers = [];
          foreach ($sheuser as $key => $value) {
              $userlist = M('member')->where($value)->select();
              // dump($userlist);
              foreach ($userlist as $ukey => $uval) {
                  if ($uval['ment'] == '远大商学院') {
                    $susers[] = $uval;
                  }
                  if ($uval['ment'] == '集团绩效管理委员会') {
                      $jusers[] = $uval;
                  }
              }
          }
          //得到学习成长评分人
          // dump($susers);
          // dump($jusers);
          // exit;
          // dump($sxy);
          // dump($jx);
          // exit;
          //
          //数据来源-商学院
          $slist = [];
          foreach ($sxy as $sey => $s) {
              foreach ($ulist as $uey => $u) {
                  $stmp = $s;
                  $stmp['pname'] = $sname2['name'];
                  $stmp['uname'] = $u['user'];
                  $stmp['ment'] = $u['ment'];
                  $stmp['uid'] = $u['uid'];
                  $stmp['bming'] = '绩效考核表';
                  // $tmp['m_type'] = '1';
                  $stmp['pid'] = $s['id'];
                  $stmp['k_time'] = $s['j_time'];
                  unset($stmp['id']);
                  unset($stmp['bu']);
                  unset($stmp['type']);
                  unset($stmp['j_time']);
                  unset($stmp['j_name']);
                  $slist[] = $stmp;
              }
              
          }
          // dump($slist);
          // exit;
          //数据来源-绩效部门
          $jlist = [];
          foreach ($jx as $jey => $j) {
              foreach ($ulist as $uey => $u) {
                  $jtmp = $j;
                  $jtmp['pname'] = $sname2['name'];
                  $jtmp['uname'] = $u['user'];
                  $jtmp['ment'] = $u['ment'];
                  $jtmp['uid'] = $u['uid'];
                  $jtmp['bming'] = '绩效考核表';
                  // $tmp['m_type'] = '1';
                  $jtmp['pid'] = $j['id'];
                  $jtmp['k_time'] = $j['j_time'];
                  unset($jtmp['id']);
                  unset($jtmp['bu']);
                  unset($jtmp['type']);
                  unset($jtmp['j_time']);
                  unset($jtmp['j_name']);
                  $jlist[] = $jtmp;
              }
              
          }
         //其他部门
         $qlist = [];
          foreach ($qt as $qey => $q) {
              foreach ($ulist as $uey => $u) {
                  $qtmp = $q;
                  $qtmp['pname'] = $sname2['name'];
                  $qtmp['uname'] = $u['user'];
                  $qtmp['ment'] = $u['ment'];
                  $qtmp['uid'] = $u['uid'];
                  $qtmp['bming'] = '绩效考核表';
                  // $tmp['m_type'] = '1';
                  $qtmp['pid'] = $q['id'];
                  $qtmp['k_time'] = $q['j_time'];
                  unset($qtmp['id']);
                  unset($qtmp['bu']);
                  unset($qtmp['type']);
                  unset($qtmp['j_time']);
                  unset($qtmp['j_name']);
                  $qlist[] = $qtmp;
              }
              
          }
          // if (!empty($slist)) {
                
          // }
            // dump($sxy);
            // echo '<hr>';
            // dump($slist);
            // exit;
            //如果数据来源有商学院
            if (!empty($slist)) {
                $sres = [];
                foreach ($slist as $sey => $sv) {
                    foreach ($susers as $ukey => $su) {
                          $sutmp = $sv;
                          unset($sutmp['pname']);
                          $sutmp['pname'] =  $su['user'];
                          $sres[] = $sutmp;
                    }
                }
                // dump($sres);
                // exit;
                M()->startTrans();
                foreach ($sres as $value) {
                    if (M('ping')->add($value) == 'false') {
                        M()->rollback();
                        return $this->error('添加失败');
                    }
                 
                }
                M()->commit();
                foreach ($susers as $key => $value) {
                    $snames .= $value['user'].',';
                }
                $snames = rtrim($snames,', ');
                // dump()
                $spusers = [];
                foreach ($slist as $key => $value) {
                    $sptmp = $value;
                    $sptmp['pname'] = $snames;
                    $spusers[] = $sptmp;
                  
                }
                // dump($spusers);
                // exit;
                 M()->startTrans();
                foreach ($spusers as $value) {
                    if (M('people')->add($value) == 'false') {
                        M()->rollback();
                        return $this->error('添加失败');
                    }
                 
                }
                M()->commit();
                //如果数据来源有绩效
                if (!empty($jlist)) {
                    $jres = [];
                    foreach ($jlist as $jey => $jv) {
                        foreach ($jusers as $jkey => $ju) {
                              $jutmp = $jv;
                              unset($jutmp['pname']);
                              $jutmp['pname'] =  $ju['user'];
                              $jres[] = $jutmp;
                        }
                    }
                    // dump($jres);
                    // exit;
                    M()->startTrans();
                    foreach ($jres as $value) {
                        if (M('ping')->add($value) == 'false') {
                            M()->rollback();
                            return $this->error('添加失败');
                        }
                     
                    }
                    M()->commit();
                    foreach ($jusers as $key => $value) {
                        $jnames .= $value['user'].',';
                    }
                    $jnames = rtrim($jnames,', ');
                    $jpusers = [];
                    foreach ($jlist as $key => $value) {
                        $jptmp = $value;
                        $jptmp['pname'] = $jnames;
                        $jpusers[] = $jptmp;
                      
                    }
                    // dump($jpusers);
                    // exit;
                     M()->startTrans();
                    foreach ($jpusers as $value) {
                        if (M('people')->add($value) == 'false') {
                            M()->rollback();
                            return $this->error('添加失败');
                        }
                     
                    }
                    M()->commit();
                }
            }else if(!empty($jlist)){ //如果数据来源有绩效
                          $jres = [];
                          foreach ($jlist as $jey => $jv) {
                              foreach ($jusers as $jkey => $ju) {
                                    $jutmp = $jv;
                                    unset($jutmp['pname']);
                                    $jutmp['pname'] =  $ju['user'];
                                    $jres[] = $jutmp;
                              }
                          }
                          // dump($jres);
                          // exit;
                          M()->startTrans();
                          foreach ($jres as $value) {
                              if (M('ping')->add($value) == 'false') {
                                  M()->rollback();
                                  return $this->error('添加失败');
                              }
                           
                          }
                          M()->commit();
                          foreach ($jusers as $key => $value) {
                              $jnames .= $value['user'].',';
                          }
                          $jnames = rtrim($jnames,', ');
                          $jpusers = [];
                          foreach ($jlist as $key => $value) {
                              $jptmp = $value;
                              $jptmp['pname'] = $jnames;
                              $jpusers[] = $jptmp;
                            
                          }
                           M()->startTrans();
                          foreach ($jpusers as $value) {
                              if (M('people')->add($value) == 'false') {
                                  M()->rollback();
                                  return $this->error('添加失败');
                              }
                           
                          }
                          M()->commit();
            }
            // dump($jpusers);
            // // echo $snames;
            // exit;




              // dump($F);
           
              //F  添加评分人 整合数组;
              $flist = [];
              foreach ($F as $fey => $f) {
                  foreach ($ulist as $uey => $u) {
                      $tmp = $f;
                      $tmp['pname'] = $sname['name'];
                      $tmp['uname'] = $u['user'];
                      $tmp['ment'] = $u['ment'];
                      $tmp['uid'] = $u['uid'];
                      $tmp['i'] = '1';
                      $tmp['bming'] = '绩效考核表';
                      // $tmp['m_type'] = '1';
                      $tmp['pid'] = $f['id'];
                      $tmp['k_time'] = $f['j_time'];
                      unset($tmp['id']);
                      unset($tmp['bu']);
                      unset($tmp['type']);
                      unset($tmp['j_time']);
                      unset($tmp['j_name']);
                      $flist[] = $tmp;
                  }
                  
              }
              //C  添加评分人 整合数组
              $clist = [];
              foreach ($C as $cey => $c) {
                  foreach ($ulist as $uey => $u) {
                      $ctmp = $c;
                      $ctmp['xname'] = $u['lead'];
                      $ctmp['pname'] = '';
                      $ctmp['uname'] = $u['user'];
                      $ctmp['ment'] = $u['ment'];
                      $ctmp['bming'] = '绩效考核表';
                      $ctmp['uid'] = $u['uid'];
                      // $tmp['m_type'] = '1';
                      $ctmp['pid'] = $c['id'];
                      $ctmp['k_time'] = $c['j_time'];
                      unset($ctmp['id']);
                      unset($ctmp['bu']);
                      unset($ctmp['time']);
                      unset($ctmp['type']);
                      unset($ctmp['j_time']);
                      unset($ctmp['j_name']);
                      $clist[] = $ctmp;
                  }
                  
              }


              //I 添加评分人 整合数组；
              foreach ($I as $iey => $vi) {
                  if ($vi['num'] == '集团财务中心') {
                      $cai[] = $vi;
                  }
                  if ($vi['num'] != '集团财务中心') {
                      $qita[] = $vi;
                  }
              }
              //I  添加评分人 整合数组
              $ilist = [];
              foreach ($qita as $iey => $i) {
                  foreach ($ulist as $uey => $u) {
                      $itmp = $i;
                      $itmp['pname'] = $u['lead'];
                      $itmp['uname'] = $u['user'];
                      $itmp['ment'] = $u['ment'];
                      $itmp['bming'] = '绩效考核表';
                      $itmp['uid'] = $u['uid'];
                      // $itmp['i'] = '';
                      // $tmp['m_type'] = '1';
                      $itmp['pid'] = $i['id'];
                      $itmp['k_time'] = $i['j_time'];
                      unset($itmp['id']);
                      unset($itmp['bu']);
                      unset($itmp['time']);
                      unset($itmp['type']);
                      unset($itmp['j_time']);
                      unset($itmp['j_name']);
                      $ilist[] = $itmp;
                  }
                  
              }
              $iclist = [];
              foreach ($cai as $cey => $c) {
                  foreach ($ulist as $uey => $u) {
                      $ictmp = $c;
                      $ictmp['pname'] = $sname['name'];
                      $ictmp['uname'] = $u['user'];
                      $ictmp['ment'] = $u['ment'];
                      $ictmp['bming'] = '绩效考核表';
                      $ictmp['uid'] = $u['uid'];
                      $ictmp['i'] = '1';
                      // $tmp['m_type'] = '1';
                      $ictmp['pid'] = $c['id'];
                      $ictmp['k_time'] = $c['j_time'];
                      unset($ictmp['id']);
                      unset($ictmp['bu']);
                      unset($ictmp['time']);
                      unset($ictmp['type']);
                      unset($ictmp['j_time']);
                      unset($ictmp['j_name']);
                      $iclist[] = $ictmp;
                  }
                  
              }

              // dump($ilist);
              // dump($iclist);
              // exit;

              // //L  添加评分人 整合数组;
              // $llist = [];
              // foreach ($L as $ley => $l) {
              //     foreach ($ulist as $uey => $u) {
              //         $ltmp = $l;
              //         $ltmp['pname'] = $sname2['name'];
              //         $ltmp['uname'] = $u['user'];
              //         $ltmp['ment'] = $u['ment'];
              //         $ltmp['uid'] = $u['uid'];
              //         $ltmp['bming'] = '绩效考核表';
              //         // $tmp['m_type'] = '1';
              //         $ltmp['pid'] = $l['id'];
              //         unset($ltmp['id']);
              //         unset($ltmp['bu']);
              //         unset($ltmp['type']);
              //         unset($ltmp['j_time']);
              //         unset($ltmp['j_name']);
              //         $llist[] = $ltmp;
              //     }
                  
              // }

             
                
              // //如果F评分人为空,添加评分人
              if (!empty($flist)) {
                  M()->startTrans();
                  foreach ($flist as $value) {
                      if (M('people')->add($value) == 'false') {
                          M()->rollback();
                          return $this->error('添加失败');
                      }
                   
                  }
                  M()->commit();
                  $fsname = explode(",",$sname['name']);
                  // exit;
                  $fshename = [];
                  foreach ($fsname as $key => $ss) {
                        $fshename[$key]['pname'] = $ss;
                  }
                  $ffres = [];
                  foreach ($flist as $fkey => $ff) {
                      foreach ($fshename as $fkey => $she) {
                          $fres = $ff;
                          unset($fres['pname']);
                          $fres['pname'] = $she['pname'];
                          $ffres[] = $fres;
                      }
                  }
                  M()->startTrans();
                  foreach ($ffres as $value) {
                      if (M('ping')->add($value) == 'false') {
                          M()->rollback();
                          return $this->error('添加失败');
                      }
                   
                  }
                  M()->commit();
                  foreach ($F as  $vid) {
                     $id .= "`id`="."'".$vid['id']."'"." or ";
                  }
                  $id = rtrim($id,'or ');
                  $planname['pname'] = $sname['name'];
                  if (M('plan')->where($id)->save($planname) > 0) {
                      echo '1111';
                  }
              }
              //F    ------------------------END;
                  
              // //如果C评分人为空,添加评分人
              if (!empty($clist)) {
                  M()->startTrans();
                  foreach ($clist as $value) {
                      if (M('people')->add($value) == 'false') {
                          M()->rollback();
                          return $this->error('添加失败');
                      }
                   
                  }
                  M()->commit();
                 
              }
              //C    ------------------------END;

              // //如果I评分人为空,添加评分人
              if (!empty($ilist)) {
                  foreach ($ilist as  $vid) {
                     $iid .= "`id`="."'".$vid['pid']."'"." or ";
                  }
                  $iid = rtrim($iid,'or ');
                  // echo $iid;
                  // exit;
                  M()->startTrans();
                  foreach ($ilist as $ikey => $value) {
                      if (M('ping')->add($value) == 'false') {

                          M()->rollback();
                          return $this->error('添加失败');
                      }
                      if (M('people')->add($value) == 'false') {
                          M()->rollback();
                          return $this->error('添加失败');
                      }
                      $name[] = $value['pname'];
                  }
                  M()->commit();
                  $namet = array_unique($name);
                  // $iidt = array_unique($iid);
                  $ipname['pname'] = implode(',',$namet);
                  if (M('plan')->where($iid)->save($ipname) > 0) {
                      echo '1111';
                  }
              }

              if (!empty($iclist)) {
                  foreach ($iclist as  $vid) {
                     $icid .= "`id`="."'".$vid['pid']."'"." or ";
                  }
                  $icid = rtrim($icid,'or ');
                  // echo $iid;
                  // exit;
                  M()->startTrans();
                  foreach ($iclist as $ikey => $value) {
                      if (M('ping')->add($value) == 'false') {

                          M()->rollback();
                          return $this->error('添加失败');
                      }
                      if (M('people')->add($value) == 'false') {
                          M()->rollback();
                          return $this->error('添加失败');
                      }
                      $cname[] = $value['pname'];
                  }
                  M()->commit();
                  $cnamet = array_unique($cname);
                  // $iidt = array_unique($iid);
                  $icpname['pname'] = implode(',',$cnamet);
                  // echo $iid;
                  // dump($ipname);
                  // exit;
                  if (M('plan')->where($icid)->save($icpname) > 0) {
                      echo '1111';
                  }
              }
              //I    ------------------------END;
              


              // // //如果L评分人为空,添加评分人
              // if (!empty($llist)) {
              //     M()->startTrans();
              //     foreach ($llist as $value) {
              //         if (M('people')->add($value) == 'false') {
              //             M()->rollback();
              //             return $this->error('添加失败');
              //         }
                   
              //     }
              //     M()->commit();
              //     $ssname = explode(",",$sname2['name']);
              //     // exit;
              //     $shename = [];
              //     foreach ($ssname as $key => $ss) {
              //           $shename[$key]['pname'] = $ss;
              //     }
              //     $res = [];
              //     foreach ($llist as $likey => $li) {
              //         foreach ($shename as $skey => $she) {
              //             $lres = $li;
              //             unset($lres['pname']);
              //             $lres['pname'] = $she['pname'];
              //             $res[] = $lres;
              //         }
              //     }
              //     M()->startTrans();
              //     foreach ($res as $value) {
              //         if (M('ping')->add($value) == 'false') {
              //             M()->rollback();
              //             return $this->error('添加失败');
              //         }
                   
              //     }
              //     M()->commit();
              //     foreach ($L as  $vid) {
              //        $lid .= "`id`="."'".$vid['id']."'"." or ";
              //     }
              //     $lid = rtrim($lid,'or ');
              //     $lllname['pname'] = $sname2['name'];
              //     if (M('plan')->where($lid)->save($lllname) > 0) {
              //         echo '1111';
              //     }
              // }
              // //L    ------------------------END;
              

              //获得没有评分人的考核
              // dump($ulist);
              $this->redirect('System/index');
            // $plist = ($pid).'and uname='.$_POST['post'];
          
              }
            // }
      
      
    }





    public function gxuan()
    {
        // p(I());
        // dump(I());
        // exit;
        if (empty($_POST['xiu'])) {
            $list['name'] = implode(',',$_POST['user']);
            $list['type'] = $_POST['type'];
            $list['time'] = date('Y-m-d');
            if (M('she')->add($list) > 0) {
                echo '1111';
            }else{
                echo '2222';
            }
        }else{
            // P(I());
            $list['name'] = implode(',',$_POST['user']);
            // $list['type'] = $_POST['type'];
            $list['time'] = date('Y-m-d');
            // dump($list);
            // exit;
            if (M('she')->where(['type'=>$_POST['type']])->save($list) > 0) {
                echo '1111';
            }else{
                echo '2222';
            }
        }

    }





    public function xiu()
    {
      // dump(I());
      // exit;
      $list['result'] = $_POST['result'];
      $list['standard'] = $_POST['standard'];
      $list['score'] = $_POST['score'];
      if (M('people')->where(['pid'=>$_POST['id']])->save($list) > 0) {
          echo '1111';
      }else{
          echo '2222';
      }
    }






    /**
     * [cha 查询最终得分]
     * @return [type] [description]
     */
    public function cha()
    {
      // dump(I());
      // exit;
      $zlist = M('zong')->where(['id'=>$_POST['id'],'k_time'=>$_POST['time']])->find();
      $plist = M('people')->where(['uname'=>$zlist['uname'],'yue'=>$_POST['time']])->select();
      // dump($plist);
      // exit;
      foreach ($plist as $key => $value) {
        $zong += $value['score']; 
      }
      $score['score'] = $zong;
      if (!empty($zong)) {
          if (M('zong')->where(['id'=>$_POST['id'],'k_time'=>$_POST['time']])->save($score) > 0) {
            $this->ajaxReturn($zong);
          }else{
             echo '2222';
          }
          
      }else{
        echo '3333';
      }

      // echo $zong;
      // exit;
      // if (!empty($zlist)) {
      //     if (!empty($zlist['score'])) {
      //         $this->ajaxReturn($zlist['score']);
      //     }else{
      //         echo '2222';
      //     }
      // }else{
      //    echo '3333';
      // }
         

    }




    /**
     * [del 表格删除]
     * @return [type] [description]
     */
    public function del()
    {
      // dump(I());
      // exit;
      $zlist = M('zong')->where($_POST)->find();
      // dump($zlist);
      // exit;
      $pids = explode(",",$zlist['pid']);
      // dump($pids);
      // exit;
      foreach ($pids as $key => $value) {
        $pid .= "`id`="."'".$value."'"." or ";
        $ppid .= "`pid`="."'".$value."'"." or ";
      }
      $pid = rtrim($pid,'or ');
      $ppid = '('. rtrim($ppid,'or ').')'.' AND '.'('."`uname` = "."'".$zlist['uname']."'".')';
      $tables = M('plan')->where($pid)->select();
      // dump($tables);
      if (empty($tables[0]['uname'])) {
          
          if (M('ping')->where($ppid)->delete() > 0) {
              if (M('people')->where($ppid)->delete() > 0) {
                if (M('zong')->where(['id'=>$_POST['id']])->delete() > 0) {
                    echo '1111';
                }
              }
          }else{
              echo '2222';
              exit;
          }
      }else{
          if (M('plan')->where($pid)->delete() > 0) {
              if (M('ping')->where($ppid)->delete() > 0) {
                  if (M('people')->where($ppid)->delete() > 0) {
                    if (M('zong')->where(['id'=>$_POST['id']])->delete() > 0) {
                        echo '1111';
                    }
                  }
              }
           }else{
              echo '2222';
              exit;
           }
        
      }



    }




    /**
     * [sou 用户搜索]
     * @return [uname] [用户名]
     */
    public function sou()
    {
        // $list = $_POST;
        $wname = $_POST['uname'];
        // dump($wname);
        // exit;
  
        // $data['name']= array('like','%'.$wname.'%');
        $uname['uname']= array('like','%'.$wname.'%');
        // $data['x_type'] = $list['x_type'];
        if (empty($uname)) {
          // echo '1111';
          // exit;
          $wlist = M('zong')->order('uname desc')->select();
        }else{
          $wlist = M('zong')->where($uname)->order('k_time desc')->select();
        }
        
        // dump($wlist);
        // exit;
        if (!empty($wlist)) {
           // $arr = M('stock')->where($data)->select();
            $this->ajaxReturn($wlist);
        }else{
          echo '1111';
        }
    // dump(I());
      // $zonglist = M('zong')->where(['uname'=>$_POST['uname']])

    }


    /**
     * [cha 选项查询操作]
     * @return [ment] [部门]
     * @return [k_time] [绩效月]
     */
    public function xcha()
    {
      // dump(I());
      // exit;
      if ($_POST['type'] == '0') {
          if ($_POST['ment'] == '0' && $_POST['k_time'] == '0') {
              $zonglist = M('zong')->order('uname desc')->select();
              $this->ajaxReturn($zonglist);   
          }else if($_POST['ment'] != '0' && $_POST['k_time'] == '0'){
              $zonglist = M('zong')->where(['ment'=>$_POST['ment']])->order('ment desc')->select();
              $this->ajaxReturn($zonglist);
          }else if($_POST['ment'] == '0' && $_POST['k_time'] != '0'){
              $zonglist = M('zong')->where(['k_time'=>$_POST['k_time']])->order('ment desc')->select();
              $this->ajaxReturn($zonglist);
          }else if($_POST['ment'] != '0' && $_POST['k_time'] != '0'){
              $zonglist = M('zong')->where(['k_time'=>$_POST['k_time'],'ment'=>$_POST['ment']])->order('ment desc')->select();
              $this->ajaxReturn($zonglist);
          }
      }else{
          if ($_POST['ment'] == '0' && $_POST['k_time'] == '0') {
              $zonglist = M('zong')->order('uname desc')->select();
              $this->ajaxReturn($zonglist);   
          }else if($_POST['ment'] != '0' && $_POST['k_time'] == '0'){
              $zonglist = M('zong')->where(['ment'=>$_POST['ment']])->order('ment desc')->select();
              $this->ajaxReturn($zonglist);
          }else if($_POST['ment'] == '0' && $_POST['k_time'] != '0'){
              $zonglist = M('zong')->where(['k_time'=>$_POST['k_time']])->order('ment desc')->select();
              $this->ajaxReturn($zonglist);
          }else if($_POST['ment'] != '0' && $_POST['k_time'] != '0'){
              $zonglist = M('zong')->where(['k_time'=>$_POST['k_time'],'ment'=>$_POST['ment']])->order('ment desc')->select();
              $this->ajaxReturn($zonglist);
          }
      }



    }


    /**
     * [xuancha 下拉框查询]
     * @return [type] [description]
     */
    public function xuancha()
    {
      // dump(I());
      // exit;
      $pinglist = M('ping')->where($_POST)->select();
      foreach ($pinglist as $key => $value) {
          $arr[] = $value['pname'];
            // $res[] = $arr;
      }
      // dump($arr);
      // exit;
      $this->assign('arr',$arr);
      // $this->display('jiahua');
    }


    


}

